<template>
  <el-dialog title="分配权限" :visible="showAss" @close="openShow">

    <el-tree
      ref="list"
      node-key="id"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      default-expand-all
      highlight-current
    />
    <el-button @click="add">保存</el-button>
  </el-dialog>
</template>

<script>
import { addPermission, upPermission, getPermissionList, assignPermission } from '@/api/permission'
import { arr2tree } from '@/utils'
export default {
  props: {
    showAss: {
      type: Boolean
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permList: []
    }
  },
  created() {
    this.PermissionList()
  },
  methods: {
    async add() {
      const key = this.$refs.list.getCheckedKeys()
      console.log(key)

      await assignPermission({ id: this.curId, permIds: key })
      this.openShow()
    },
    async getPrem() {
      const res = await upPermission(this.curId)
      console.log(res)
      this.$refs.list.setCheckedKeys(res.data.permIds)
    },
    async PermissionList() {
      const res = await getPermissionList()
      this.list = arr2tree(res.data, 0)
    },
    async addPermission() {
      await addPermission()
    },
    openShow() {
      this.$emit('update:showAss', false)
    }
  }
}
</script>

<style>

</style>
